package com.toocms.jinjuhang.dialog.options

import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.widget.view_model.showToast
import com.toocms.tab.binding.ItemBinding
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand

class OptionsModel(val dialog: OptionsDialog) {
    val isShowConfirmObservableBoolean = ObservableBoolean(false)
    val items = ObservableArrayList<OptionsItemModel>()
    var selectedOptions: OptionsItem? = null
    val itemBinding = ItemBinding.of<OptionsItemModel>(BR.optionsItemModel, R.layout.item_options)
    var titleObservableField = ObservableField<String>()
    var optionsItems: List<out OptionsItem>? = null
        set(value) {
            field = value
            items.clear()
            value?.forEach {
                items += OptionsItemModel(this@OptionsModel, it)
            }
        }

    val closeClickBindingCommand = BindingCommand<BindingAction>(dialog::dismiss)

    val confirmClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        if (null == selectedOptions) {
            val title = titleObservableField.get() ?: "选项"
            if (title.startsWith("请选择")) {
                dialog.showToast(title)
            } else {
                dialog.showToast("请选择${title}")
            }
            return@BindingAction
        }
        dialog?.onOptionClickListener?.invoke(selectedOptions!!, dialog.tag)
        dialog?.dismiss()
    })

    init {

    }

}